<template>
  <!-- 顶部信息栏 -->
  <div class="top_information">
    <div class="logo_area" @click="router.push('/home')">
      <el-icon :size="60" color="#736EFE">
        <Sell/>
      </el-icon>
      <span class="shop_name">我的商城</span>
    </div>
    <div class="user_info">
      <img :src="data.user.avatar || default_avatar" alt="用户头像" class="user_avatar"/>
      <span class="user_name">{{ data.user.shopname }}</span>
      <el-button @click="logout" type="primary" size="large" class="logout_btn">退出登录</el-button>
    </div>
  </div>
  <!-- 中间页面 -->
  <div class="main_content">
    <!-- 左侧导航 -->
    <div class="navigator">
      <el-menu
          router
          :default-active="currentRoute"
          style="border: none"
          :default-openeds="['1']"
          active-text-color="#ffd04b"
          background-color="#2C3E50"
          class="el-menu-vertical-demo"
          text-color="#fff"
      >
        <el-menu-item index="/user/person">
          <el-icon>
            <Avatar/>
          </el-icon>
          个人信息
        </el-menu-item>
        <el-menu-item index="/user/order">
          <el-icon>
            <Setting/>
          </el-icon>
          <span>我的订单</span>
        </el-menu-item>
        <el-menu-item index="/news">
          <el-icon>
            <Document/>
          </el-icon>
          <span>最新消息</span>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 右侧主页内容 -->
    <div class="main_content_area">
      <RouterView :key="router" @updatePerson="updatePerson"></RouterView>
    </div>
  </div>
</template>

<script setup>
import {reactive, computed} from "vue";
import {useRoute} from "vue-router";
import {
  Sell,
  House,
  Present,
  List,
  Operation,
  Setting,
  Document, Avatar, DataAnalysis
} from "@element-plus/icons-vue";
import router from "@/router/index.js";
import default_avatar from '@/assets/img/default_avatar.png'

const data = reactive({
  user: JSON.parse(localStorage.getItem("user_information") || "{}"), // 默认为空对象
  isCollapse: false
});

// 计算当前激活的路由路径
const currentRoute = computed(() => useRoute().path);

const logout = () => {
  localStorage.removeItem("user_information");
  location.href = "/login";
};
const updatePerson = () => {
  data.user = JSON.parse(localStorage.getItem("user_information"));
}
</script>

<style scoped>
.top_information {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #736EFE, #A8A4FF);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.logo_area {
  display: flex;
  align-items: center;
}

.shop_name {
  font-size: 26px;
  color: #ffffff;
  margin-left: 8px;
  font-weight: bold;
}

.user_info {
  display: flex;
  align-items: center;
}

.user_avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.user_name {
  font-size: 20px;
  color: #ffffff;
  margin-right: 20px;
}

.logout_btn {
  background-color: #FF6B6B;
  border: none;
  font-size: 16px;
  padding: 10px 20px;
}

.main_content {
  display: flex;
  height: calc(100vh - 100px);
}

.navigator {
  width: 200px;
  background-color: #2C3E50;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
}

.main_content_area {
  flex: 1;
  padding: 20px;
  background-color: #F5F5F5;
  overflow-y: auto;
}

.el-menu-vertical-demo {
  border-right: none;
}

.el-menu-item {
  padding: 15px 20px;
  font-size: 16px;
}

.el-menu-item:hover {
  background-color: #34495E;
}

.el-menu-item.is-active {
  background-color: #1ABC9C;
}
</style>